home *** CD-ROM | disk | FTP | other *** search
- From: phalpern@truffle.ultranet.com (Pablo Halpern)
- Message-ID: <4gfg2f$iga@caesar.ultra.net>
- X-Original-Date: Wed, 21 Feb 1996 16:08:45 GMT
- Path: in2.uu.net!bounce-back
- Date: 21 Feb 96 16:13:11 GMT
- Approved: fjh@cs.mu.oz.au
- Newsgroups: comp.std.c++
- Subject: Re: Q: Generic Callbacks -- "Object->*func(...)"
- Organization: UltraNet Communications, Inc.
- References: <4fti32$p3p@bcarh8ab.bnr.ca>
- X-Newsreader: Forte Agent .99b.113
- X-Auth: PGPMoose V1.1 PGP comp.std.c++
- iQBFAgUBMStEq+EDnX0m9pzZAQERygF/UO4VTWYjxJ/ToYm40NnAgzsovSXK5vtn
- 6FvwgPRlKUq4QK5isx/WAaTY49B8YPJA
- =2Yzj
-
- "brian (b.c.) white" <bcwhite@bnr.ca> wrote:
-
- >Does the C++ standard allow for a generic callback to be specified?
- >
- >Basically, I'd like to be able to pass an arbitrary object and function of
- >that object to be called at some later time. For example:
-
- This is a problem I'd like to see addressed directly in the language or
- in the standard library (although I doubt it will be).
-
- A former collegue of mine suggested to me that the semantics of taking
- the address of a member function in C++ could be much more powerfull
- than they are. In C++, a function is not bound until it is invoked using
- the object.function() or pointer->function() construct. What he
- suggested is that there be a special type of pointer that refers to a
- bound function. (He called it a closure, although I'm not sure it meets
- the precise definition of closure, since parameters are not specified.)
- Usage would be something like this:
-
- // The use of the "bound" keyword, below, is for illustrative purposes
- // only and is not intended as a syntax proposal.
- typedef bound void (*boundfunc)(args); // Pointer to bound function
-
- class firstClass
- {
- public:
- void func1(args);
- void func2(args);
- };
-
- class secondClass
- {
- public:
- void func3(args);
- };
-
- f()
- {
- firstClass firstObj;
- secondClass secondObj;
- boundfunc pf = &(firstObj.func1);
- pf(actual args); // calls firstObj.func1(actual args)
- pf = &(secondObj.func3);
- pf(actual args); // calls secondObj.func3(actual args)
- }
-
- The address of functions will be either a normal pointer to function, a
- pointer to member function, or a pointer to bound function as follows:
-
- &globalFunc // pointer to global/static function
- &firstClass::func2 // pointer to member function (of firstClass)
- &firstObj.func2 // pointer to bound function (bound to firstObj)
- &pointer->func1 // pointer to bound function (bound to *pointer)
-
- I believe that the above could be a reasonable addition to the language:
-
- 1. It is useful
- 2. I think it is easy to implement. The internal structure of a bound
- pointer is basically an object pointer and a pointer to the actual code
- of the member function (no need for offsets or vtbl references, since
- the binding has already been done). All object offset calculations are
- done before the object pointer is stored. Global and static function
- pointers can be stored in a bound pointer by setting the object pointer
- to NULL.
- 3. It will not break existing code (the &(object.function) notation is
- currently illegal).
- 4. I'm not sure that this cannot be achieved any other way, but I think
- that it may not be achievable efficiently any other way.
-
- -------------------------------------------------------------
- Pablo Halpern phalpern@truffle.ultranet.com
-
- I am self-employed. Therefore, my opinions *do* represent
- those of my employer.
- ---
- [ To submit articles: try just posting with your news-reader.
- If that fails, use mailto:std-c++@ncar.ucar.edu
- FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
- Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
- Comments? mailto:std-c++-request@ncar.ucar.edu.
- ]
-